Completed
Push — master ( 165560...d3be23 )
by Denis
31s
created

jets.js ➔ ... ➔ options.hideBy.map   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 1
rs 10
1
/*! Jets.js - v0.11.0 - 2016-07-30
2
* http://NeXTs.github.com/Jets.js/
3
* Copyright (c) 2015 Denis Lukov; Licensed MIT */
4
5
;(function(root, definition) {
6
  if (typeof module != 'undefined') module.exports = definition();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
7
  else if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
8
  else root['Jets'] = definition();
9
}(this, function() {
10
  "use strict"
11
12
  function Jets(opts) {
13
    if( ! (this instanceof Jets)) {
14
      return new Jets(opts);
15
    }
16
    var self = this;
17
    ['searchTag', 'contentTag'].forEach(function(param) {
18
      var name = param.replace('Tag', ''),
19
        queryMethod = 'querySelector' + (param == 'contentTag' ? 'All' : '');
20
      self[name + '_tag'] = document[queryMethod](opts[param]);
21
      self[name + '_param'] = opts[param];
22
      if( ! self[name + '_tag']) {
23
        throw new Error('Error! Could not find ' + param + ' element');
24
      }
25
    });
26
27
    var defaults = {
28
      searchSelector: '*AND',
29
      hideBy: 'display:none',
30
      diacriticsMap: {}
31
    }
32
33
    self.options = {};
34
    ['columns', 'addImportant', 'searchSelector', 'hideBy', 'manualContentHandling', 'callSearchManually', 'diacriticsMap', 'didSearch', 'invert'].forEach(function(name) {
35
      self.options[name] = opts[name] || defaults[name];
36
    });
37
    if(this.options.searchSelector.length > 1) {
38
      var searchSelector = self.options['searchSelector'].trim();
39
      self.options.searchSelector = searchSelector.substr(0, 1);
40
      self.options.searchSelectorMode = searchSelector.substr(1).toUpperCase();
41
    }
42
43
    var last_search_query;
44
    self.search = function() {
45
      if(last_search_query == (last_search_query = self.search_tag.value)) return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
46
      (0,self._applyCSS());
0 ignored issues
show
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
47
      self.options.didSearch && self.options.didSearch(self.search_tag.value);
48
    };
49
    self._onSearch = function(event) {
50
      if(event.type == 'keydown')
51
        return setTimeout(self.search, 0);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
52
      self.search();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
53
    };
54
    self.destroy = function() {
55
      if( ! self.options.callSearchManually) self._processEventListeners('remove');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
56
      self._destroy();
57
    };
58
59
    if( ! self.options.callSearchManually) self._processEventListeners('add');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
60
    self._addStyleTag();
61
    self._setJets();
62
    self._applyCSS();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
63
  }
64
65
  Jets.prototype = {
66
    constructor: Jets,
67
    _processEventListeners: function(action) {
68
      ['input', 'keydown', 'change'].forEach(function(event_type) {
69
        this.search_tag[action + 'EventListener'](event_type, this._onSearch);
70
      }.bind(this));
71
    },
72
    _applyCSS: function() {
73
      var options = this.options,
74
        search_phrase = this.replaceDiacritics(this.search_tag.value.trim().toLowerCase().replace(/\s\s+/g, ' ')).replace(/\\/g, '\\\\'),
75
        words = options.searchSelectorMode
76
          ? search_phrase.split(' ').filter(function(item, pos, arr) { return arr.indexOf(item) == pos; })
77
          : [search_phrase],
78
        is_strict_selector = options.searchSelectorMode == 'AND',
79
        selectors = new Array(words.length);
80
      for(var i = 0, ii = words.length; i < ii; i++) {
81
        selectors[i] = (is_strict_selector ? this.content_param + '>' : '') + (options.invert ? '' : ':not(') + '[data-jets' +
82
          options.searchSelector + '="' + words[i] + '"]' + (options.invert ? '' : ')');
83
      }
84
      var hide_rules = options.hideBy.split(';').filter(Boolean).map(function(rule) { return rule + (options.addImportant ? '!important' : '') });
85
      var css_rule = (is_strict_selector ? '' : this.content_param + '>') + selectors.join(is_strict_selector ? ',' : '') + '{' + hide_rules.join(';') + '}';
86
      this.styleTag.innerHTML = search_phrase.length ? css_rule : '';
87
    },
88
    _addStyleTag: function() {
89
      this.styleTag = document.createElement('style');
90
      document.head.appendChild(this.styleTag);
91
    },
92
    _getText: function(tag) {
93
      return tag && (tag.textContent || tag.innerText) || '';
94
    },
95
    _getContentTags: function(query) {
96
      return Array.prototype.slice.call(this.content_tag).reduce(function(all, elem) {
97
        return all.concat(Array.prototype.slice.call(elem.querySelectorAll(query || ':scope > *')));
98
      }, []);
99
    },
100
    _setJets: function(query, force) {
101
      var self = this,
102
        tags = self._getContentTags(force ? '' : query), text;
103
      for(var i = 0, tag; tag = tags[i]; i++) {
104
        if(tag.hasAttribute('data-jets') && ! force) continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
105
        text = this.options.manualContentHandling
106
          ? this.options.manualContentHandling(tag)
107
          : self.options.columns && self.options.columns.length
108
            ? self.options.columns.map(function(column) {
109
                return self._getText(tag.children[column]);
0 ignored issues
show
Bug introduced by
The variable tag is changed as part of the for loop for example by tags.i on line 103. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
110
              }).join(' ')
111
            : self._getText(tag);
112
        tag.setAttribute('data-jets', self.replaceDiacritics(text.trim().replace(/\s+/g, ' ').toLowerCase()));
113
      };
114
    },
115
    replaceDiacritics: function(text) {
116
      var diacritics = this.options.diacriticsMap;
117
      for(var letter in diacritics) if(diacritics.hasOwnProperty(letter)) {
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
118
        for(var i = 0, ii = diacritics[letter].length; i < ii; i++) {
119
          text = text.replace(new RegExp(diacritics[letter][i], 'g'), letter);
120
        }
121
      }
122
      return text;
123
    },
124
    update: function(force) {
125
      this._setJets(':scope > :not([data-jets])', force);
126
    },
127
    _destroy: function() {
128
      this.styleTag.parentNode && document.head.removeChild(this.styleTag);
129
      var tags = this._getContentTags();
130
      for(var i = 0, tag; tag = tags[i]; i++) {
131
        tag.removeAttribute('data-jets');
132
      }
133
    }
134
  }
135
136
  // :scope polyfill
137
  // http://stackoverflow.com/a/17989803/1221082
138
  ;(function(doc, proto) {
139
    try {
140
      doc.querySelector(':scope body');
141
    } catch (err) {
142
      ['querySelector', 'querySelectorAll'].forEach(function(method) {
143
        var nativ = proto[method];
144
        proto[method] = function(selectors) {
145
          if (/(^|,)\s*:scope/.test(selectors)) {
146
            var id = this.id;
147
            this.id = 'ID_' + Date.now();
148
            selectors = selectors.replace(/((^|,)\s*):scope/g, '$1#' + this.id);
149
            var result = doc[method](selectors);
150
            this.id = id;
151
            return result;
152
          } else {
153
            return nativ.call(this, selectors);
154
          }
155
        }
156
      });
157
    }
158
  })(window.document, Element.prototype);
0 ignored issues
show
Bug introduced by
The variable Element seems to be never declared. If this is a global, consider adding a /** global: Element */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
159
160
  return Jets;
161
}));
162